package ysq.xyg.host.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.extend.ehcache.EhcacheUtils;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostNoticeDao;
import ysq.xyg.host.model.dto.HostNoticeDto;
import ysq.xyg.host.model.po.HostNotice;
import ysq.xyg.host.model.vo.HostNoticeSearchVo;
import ysq.xyg.host.model.vo.HostNoticeVo;
import ysq.xyg.host.service.HostNoticeService;
import ysq.xyg.sys.model.po.SysUser;

import java.util.Date;
import java.util.List;

/**
 * 房产-通知公告service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:08:31
 */
@Service("hostNoticeService")
public class HostNoticeServiceImpl extends BaseServiceImpl<HostNotice> implements HostNoticeService {

    @Autowired
    private HostNoticeDao hostNoticeDao;

    @Autowired
    public void setBaseDao(HostNoticeDao hostNoticeDao) {
        this.baseDao = hostNoticeDao;
    }

    /**
     * 分页查询
     *
     * @param hostNoticeVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:08:31
     */
    @Override
    public ReturnEntity page(HostNoticeVo hostNoticeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" order_num asc, create_time desc");
        List<HostNotice> list = acceptList(hostNoticeVo);
        // 需要城市名称
        if (CollUtil.isNotEmpty(list)) {
            for (HostNotice hostNotice : list) {
                hostNotice.setCityName(EhcacheUtils.getCityNameById(hostNotice.getAccountId(), hostNotice.getCityId()));
            }
        }
        PageInfo<HostNotice> page = new PageInfo<>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageHostNoticeMobile(HostNoticeSearchVo hostNoticeSearchVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<HostNoticeDto> list = hostNoticeDao.selectEntitySearchList(hostNoticeSearchVo);
        if (CollUtil.isNotEmpty(list)) {
            for (HostNoticeDto hostNoticeDto : list) {
                hostNoticeDto.setSourceTypeName(EhcacheUtils.getDictContentByTypeAndValue("host_notice_source_type", hostNoticeDto.getSourceType()));
            }
        }
        PageInfo<HostNoticeDto> page = new PageInfo<>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostNoticeVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:08:31
     */
    @Override
    public ReturnEntity addOrUpdate(HostNoticeVo hostNoticeVo, SysUser currentUser) {
        boolean isUpate = true;
        HostNotice hostNotice;
        if (StrUtil.isEmpty(hostNoticeVo.getId())) {
            hostNotice = new HostNotice(currentUser);
            isUpate = false;
        } else {
            hostNotice = selectByPrimaryKey(hostNoticeVo.getId());
            if (ObjectUtil.isNull(hostNotice)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostNotice.setUpdateTime(new Date());
            hostNotice.setUpdateUser(currentUser.getId());
        }
        hostNotice.setIsCity(hostNoticeVo.getIsCity());
        hostNotice.setCityId(hostNoticeVo.getCityId());
        hostNotice.setTypeId(hostNoticeVo.getTypeId());
        hostNotice.setIsRecommend(hostNoticeVo.getIsRecommend());
        hostNotice.setName(hostNoticeVo.getName());
        hostNotice.setNum(0);
        hostNotice.setDescription(hostNoticeVo.getDescription());
        hostNotice.setContent(hostNoticeVo.getContent());
        hostNotice.setCoverImage(hostNoticeVo.getCoverImage());
        hostNotice.setImages(hostNoticeVo.getImages());
        hostNotice.setType(hostNoticeVo.getType());
        hostNotice.setOrderNum(hostNoticeVo.getOrderNum());
        hostNotice.setSourceType(hostNoticeVo.getSourceType());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostNotice);
        } else {
            num = insert(hostNotice);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:08:31
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostNotice hostNotice = selectByPrimaryKey(id);
        if (hostNotice != null) {
            hostNotice.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostNotice.setUpdateTime(new Date());
            hostNotice.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostNotice);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostNoticeVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:08:31
     */
    @Override
    public List<HostNotice> acceptList(HostNoticeVo hostNoticeVo){
        Example example = new Example(HostNotice.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostNoticeVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostNoticeVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostNoticeVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostNoticeVo.getAccountId());
        }
        if (StringUtil.isNotEmpty(hostNoticeVo.getName())) {
            criteria.andLike("name", "%" + hostNoticeVo.getName() + "%");
        }
        if (StringUtil.isNotEmpty(hostNoticeVo.getTypeId())) {
            criteria.andEqualTo("typeId", hostNoticeVo.getTypeId());
        }
        example.setOrderByClause(" order_num asc, create_time desc");
        return hostNoticeDao.selectByExample(example);
    }

    @Override
    public List<HostNoticeDto> selectHostNoticeList(String accountId, String cityId, String isRecommend, int size) {
        return hostNoticeDao.selectHostNoticeList(accountId, cityId, isRecommend, size);
    }
}
